package com.jiyun.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.exl.EasyPoiUtil;
import com.jiyun.model.chengyuan.Chengyuan;
import com.jiyun.model.jumin.Jumin;
import com.jiyun.oss.AliyunOSSUtils;
import com.jiyun.result.Result;
import com.jiyun.service.juminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import com.jiyun.vo.jumin.JuminVO;

import java.io.InputStream;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("jumin")
@Api(tags = "jumin管理模块")
public class juminController {
    @Autowired
    private juminService juminService;

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @ApiOperation(value = "查询所有居民信息")
    @PostMapping("juminFindAll")
    public Result juminFndAll(@RequestBody Jumin jumin, Integer pn, Integer ps){
        return juminService.juminFndAll(jumin, pn, ps);
    }
    @ApiOperation(value = "添加居民信息")
    @PostMapping("juminAdd")
    public void juminAdd(@RequestBody Jumin jumin,String chengyuans){
        juminService.juminAdd(jumin, chengyuans);
    }
    @ApiOperation(value = "查看居民详细信息")
    @GetMapping("findByid")
    public Result findByid(Integer id){
        return juminService.findByid(id);
    }
    @RequestMapping("uploadLocal")
    @ApiOperation(value = "图片处理")
        public String uploadLocal(MultipartFile file) throws IOException {
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
            return s;

    }
    @GetMapping("exportExcel")
    @ApiOperation(value = "导出")
    public void exportExcel(Integer[] ids, HttpServletResponse response) throws IOException {
        List<JuminVO> list = juminService.juminOut(ids);
        EasyPoiUtil.exportExcel(list, "居民信息", "sheet1", JuminVO.class, "居民导出表.xls", response);
    }
    @GetMapping("deJumin")
    @ApiOperation(value = "删除居民信息")
    public void deJumin(Integer id){
        juminService.de(id);
    }
    @GetMapping("upStatus")
    @ApiOperation(value = "更新状态")
    //不更新状态
    public void upStatus(Integer id){
        juminService.upStatus(id);
    }
    @RequestMapping("findAll")
    public List<Jumin> findAll(){
        return juminService.findAll();
    }
}
